/*
 * Copyright @ 2024 weili23
 * com.itheima.mp.controller 15:12
 * All right reserved.
 */

package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @desc:
 * @author: weili23
 * @createTime: 2024/4/16 15:12
 * @version: v1.0
 */
@Api(tags ="用户管理接口")
@RequestMapping(value = "/users")
@RestController
public class UserController {

    @Resource
    private IUserService userService;

    @ApiOperation( "新增用户")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO){
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable("id") Long id){
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") Long id){
        return userService.queryUserAndAddrById(id);
    }

    @ApiOperation("根据id批量查询用户")
    @PutMapping
    public List<UserVO> batchQueryByIds(@RequestParam("ids") List<Long> ids){
        List<User> users = userService.listByIds(ids);
        return BeanUtil.copyToList(users,UserVO.class);
    }

    @ApiOperation("根据用户id扣减金额")
    @GetMapping("/{id}/deducy/{money}")
    public void deductMoney(@ApiParam("用户id") @PathVariable("id") Long id,
                            @ApiParam("扣减金额") @PathVariable("money") Integer money){
        userService.deductMoney(id,money);
    }

    @ApiOperation("根据条件查询用户")
    @GetMapping("/byCondition")
    public List<User> queryUserbyCondition(UserQuery query){
        return userService.queryUserbyCondition(query);

    }

    @ApiOperation("根据条件分页查询用户")
    @GetMapping("/pages")
    public PageDTO<User> queryUserPagesByCondition(UserQuery query){
        return userService.queryUserPagesByCondition(query);

    }

}
